﻿using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using NewRetail.Application.Components.Account.Model;
using NewRetail.Application.Components.Goods.Entity;
using NewRetail.Application.Components.Platform.Entity;
using NewRetail.Application.Components.Warehouse.Entity;
using NewRetail.Application.Core;
using NewRetail.Application.Core.SingleStore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Domain.Repositories;

namespace NewRetail.Api.Controllers
{
    [NewRetailRoute("supplier")]
    public class SupplierController : BaseController<Supplier, SupplierPagingDto>
    {
        private readonly ISingleStoreService singleStoreService;

        public SupplierController(ISingleStoreService singleStoreService)
        {
            this.singleStoreService = singleStoreService;
        }

        protected override async Task<IQueryable<Supplier>> FilterQueryPaging(DbContext db, IQueryable<Supplier> query, SupplierPagingDto dto)
        {
            query = query.WhereIf(!string.IsNullOrWhiteSpace(dto.name), x => x.supplierName.Contains(dto.name) || x.No.Contains(dto.name));

            //单店权限
            string roleWarehouseId = this.singleStoreService.CurrentStore;
            query = query.WhereIf(!string.IsNullOrWhiteSpace(roleWarehouseId),
                x => x.warehouseID == roleWarehouseId);

            await Task.CompletedTask;
            return query;
        }

        protected override async Task<IQueryable<Supplier>> OrderQueryPaging(IQueryable<Supplier> query, SupplierPagingDto dto)
        {
            await Task.CompletedTask;

            query = query.OrderByDescending(x => x.No);
            return query;
        }

        #region add by cl 20221121
        /// <summary>
        /// 过滤已经入库或退库过的不能删除
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        protected override async Task<string[]> BeforeDelete(string[] ids)
        {
            string[] delIds = null;

            if (ids.Length > 0)
            {
                var db = await this.Repo.GetDbContextAsync();
                string[] supplierIDs = await db.Set<InOutWarehousing>().AsNoTracking().Where(g => ids.Contains(g.supplierID)).Select(g => g.supplierID).Distinct().ToArrayAsync();
                //找出没有入库或退库的供应商
                delIds = ids.Where(id => !supplierIDs.Contains(id)).ToArray();
            }
            if (delIds.Length == 0)
            {
                throw new UserFriendlyException("没有要删除的记录或供应商已经被使用！");
            }

            return delIds;
        }
        #endregion

        #region add by cl 20221130
        public override async Task<ResponseEntity<Supplier[]>> QueryAll()
        {
            var data = await base.QueryAll();
            data.ThrowIfNotSuccess();
            data.Data ??= Array.Empty<Supplier>();
            //单店权限
            string roleWarehouseId = this.singleStoreService.CurrentStore;
            data.Data = data.Data.WhereIf(!string.IsNullOrWhiteSpace(roleWarehouseId),
                x => x.warehouseID == roleWarehouseId)?.ToArray();

            data.Data = data.Data.OrderByDescending(x => x.No).ToArray();

            return data;
        }
        #endregion

    }
}
